/*************************************************************************
	> File Name: test10.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023年08月23日 星期三 21时38分16秒
 ************************************************************************/

#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    pid_t id = fork();
    if (id < 0) {
        perror("fork");
        exit(-1);
    } else if (id == 0) {
        printf("Child is run, pid is %d.\n", getpid());
        sleep(5);
        exit(25);
    } else {
        int st = 0;
        pid_t ret = waitpid(-1, &st, 0);
        printf("This is a test for wait\n");
        if (WIFEXITED(st) && ret == id) printf("Wait the child 5s success, child return code is : %d.\n", WEXITSTATUS(st));
        else printf("Wait the child failure, return.\n");
    }
    return 0;
}


